package com.chenjun.core.auth.encoder;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

import com.chenjun.common.util.ConfigUtil;

/**
 * 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author yanjinyin
 * @date 2017年7月23日 下午4:45:29
 */
public final class EncodingUtils {

	/**
	 * 
	 * @Description:对用户名和密码进行加密
	 * 暂时不适用 
	 * @param userName
	 * @param orgPwd
	 * @return
	 * @author hasee
	 * @date 2017年7月23日 下午4:45:56
	 */
	@SuppressWarnings("unused")
	private static Map<String, String> getEncodedPwdAndSalt(String userName, String orgPwd) {

		if (StringUtils.isBlank(userName) || StringUtils.isBlank(orgPwd)) {
			return null;
		}
		Map<String, String> configs = ConfigUtil.getConfigInfo(null);

		String algorithmName = configs.get("shiro.hashAlgorithmName");
		String salt1 = userName;
		String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
		int hashIterations = Integer.valueOf(configs.get("shiro.hashIterations"));

		SimpleHash hash = new SimpleHash(algorithmName, orgPwd, salt1 + salt2, hashIterations);
		final Map<String, String> result = new HashMap<String, String>(2);
		result.put("SALT", salt2);
		result.put("ENCODED_PASSWORD", hash.toHex());
		return result;
	}

	/**
	 * 对用户名和密码进行加密(不加盐)
	 * 
	 * @param userName
	 * @param orgPwd
	 * @return Map<String,String>
	 */
	public static String getEncodedPwd(String orgPwd) {
		if (StringUtils.isBlank(orgPwd)) {
			return null;
		}
		Map<String, String> configs = ConfigUtil.getConfigInfo(null);
		String algorithmName = configs.get("shiro.hashAlgorithmName");
		int hashIterations = Integer.valueOf(configs.get("shiro.hashIterations"));
		SimpleHash hash = new SimpleHash(algorithmName, orgPwd, "", hashIterations);
		String encodedPwd = hash.toHex();
		
		return encodedPwd;
	}

	private EncodingUtils() {
	}

}
